﻿//功能描述：联系人数据访问层
using Hong.IRepository.IRepository_CustomerManagement;
using Hong.Model.SysDomain.Model_CustomerManagement.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Extensions.Configuration;
using Hong.Common.Encryption;

namespace Hong.Repository.Repository.Repository_CustomerManagement
{
    public class CustInfoPersonListRepository : ICustInfoPersonListRepository
    {
        #region 构造器注入
        //注入解密SQL连接字符串
        private readonly SecurityStatic _securityStatic;
        public CustInfoPersonListRepository( SecurityStatic securityStatic)
        {
            _securityStatic = securityStatic;
        }
        #endregion
        #region 联系人查询分页
        /// <summary>
        /// 联系人查询分页
        /// </summary>
        /// <param name="personNmae"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<CustomerExPersonView> GetCustomerExPersonView(string personNmae, int pageIndex, int pageSize, out int totalCount)
        {
            using (IDbConnection conn = new SqlConnection(_securityStatic.Securityinfo()))
            {
                var sql = "select a.*,b.*,c.dicName,c.dicId,d.dicName as dicName1,d.dicId as dicId1,e.* from Customer a join Extended b on a.custId=b.customerId join Dictionaries c on b.edTypeId =c.dicId  join Dictionaries d on b.edIndustryId=d.dicId join Person e on a.custId=e.customerId where 1 =1 ";
                if (!string.IsNullOrEmpty(personNmae))
                {
                    sql += $" and personNmae='{personNmae}'";
                }
                totalCount = conn.Query<CustomerExPersonView>(sql).Count();
                sql += $"order by personId offset {pageIndex - 1}*{pageSize} rows fetch next {pageSize} row only";
                var list = conn.Query<CustomerExPersonView>(sql).ToList();
                return list;
            }
          

        }
        #endregion
    }
}
